package com.zg.common.dds;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Zxb
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    public static ThreadLocal<String> dataSourceKey = new ThreadLocal<>();
    public static Map<Object, Object> dataSourceMap = new ConcurrentHashMap<>(10);

    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceKey.get();
    }

    public static void change(String key) {
        dataSourceKey.set(key);
    }

    public static void clear() {
        dataSourceKey.remove();
    }

    public static void configDs(HikariDataSource ds){
        ds.setMinimumIdle(10);
        ds.setMaximumPoolSize(70);
        ds.setAutoCommit(true);
        ds.setMaxLifetime(1800000);
        ds.setIdleTimeout(50000);
        ds.setConnectionTimeout(60000);
        ds.setConnectionTestQuery("select 1 from dual");
        ds.addDataSourceProperty("useInformationSchema", "true");
        ds.setValidationTimeout(3000);
    }
}
